<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd()"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="list">
              <el-table-column label="序号" type="index" :index="getIndex" width="50" />
              <el-table-column label="角色名称" prop="name" width="180" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作" width="300">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :page-sizes="[1, 2, 3]"
                :page-size="params.pagesize"
                :total="total"
                layout="total,sizes,prev,pager,next,jumper"
                @current-change="hCurrentChange"
                @size-change="hSizeChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 添加编辑弹框 -->
      <el-dialog
        :title="isEdit?'编辑':'添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="resetForm"
      >
        <el-form
          ref="roleForm"
          :v-model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="medium" @click="showDialog=false">取消</el-button>
            <el-button
              size="medium"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog
        title="权限分配"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogAssign"
      >
        <AssignPermission v-if="showDialogAssign" :cur-id="id" @close="close" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, delRoles, addRoles, editRoles } from '@/api/setting'
import AssignPermission from './AssignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      // 数据
      list: [],
      // 分页
      params: {
        page: 1,
        pagesize: 2
      },
      // 表单验证，先空着
      rules: {
        name: []
      },
      // 添加编辑表单
      roleForm: {
        name: '',
        description: ''
      },
      total: 0,
      showDialog: false, // 弹窗显示隐藏
      showDialogAssign: false, // 角色分配弹框
      isEdit: false, // 添加编辑切换
      id: ''
    }
  },
  computed: {
    maxPage() {
      return Math.ceil(this.total / this.params.pagesize)
    },
    isLastPage() {
      return this.total % this.params.pagesize === 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 获取数据
    async loadRoles() {
      const res = await getRoles(this.params)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 分页
    hCurrentChange(page) {
      this.params.page = page
      this.loadRoles()
    },
    hSizeChange(page) {
      this.params.pagesize = page
      this.loadRoles()
    },
    // 分页序号
    getIndex(index) {
      return index + 1 + (this.params.page - 1) * this.params.pagesize
    },
    // 删除
    del(id) {
      this.$confirm('你确定要删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    async doDel(id) {
      try {
        const res = await delRoles(id)
        console.log(res)

        this.$message.success('删除成功')

        this.loadRoles()
      } catch (err) {
        console.log(err)
        this.$message.success('删除失败')
      }
    },
    // 新增
    hAdd() {
      this.showDialog = true
    },
    async doAdd() {
      const res = await addRoles(this.roleForm)
      console.log(res)
      if (this.isLastPage) {
        this.params.page = this.maxPage + 1
        // 为了让页码显示正确
        this.total++
      } else {
        this.params.page = this.maxPage
      }
      this.loadRoles()
      this.$message.success('添加成功')
      this.showDialog = false
    },
    // 编辑
    edit(row) {
      this.showDialog = true
      this.roleForm = { ...row }
      this.isEdit = true
    },
    async doEdit() {
      const res = await editRoles(this.roleForm)
      console.log(res)
      this.loadRoles()
      this.$message.success('编辑成功')
      this.showDialog = false
    },
    // 角色分配
    hAssign(id) {
      this.showDialogAssign = true
      this.id = id
    },
    // 切换编辑和添加
    hSubmit() {
      // 切换编辑和添加
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // 关弹窗
    resetForm() {
      // 关闭表单的时候清空
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    close() {
      this.showDialogAssign = false
    }
  }
}
</script>
